package com.sanpu.kxe.business.main.presenter;

import android.graphics.Bitmap;

import com.sanpu.kxe.business.main.bean.MaterialPosterOutput;
import com.sanpu.kxe.business.main.contract.MaterialPosterContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.jump.shareutil.ShareUtil;
import com.sanpu.kxe.jump.shareutil.share.ShareListener;
import com.sanpu.kxe.jump.shareutil.share.SharePlatform;
import com.sanpu.kxe.utils.FileUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.library.imagechoose.utils.PermissionsUtils;

import java.io.File;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：银信通推广海报
 * Created by 薛勇军 on 2019/9/28.
 */

public class MaterialPosterPresenter implements MaterialPosterContract.Presenter {

    private MaterialPosterContract.View mView;

    private CompositeSubscription mSubscription;

    public MaterialPosterPresenter(MaterialPosterContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void promotionPoster() {
        mView.showLoadingDialog(true);
        Subscription subscription = MainModel.getInstance().promotionPoster()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MaterialPosterOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<MaterialPosterOutput> posterOutputs) {
                        mView.showPosterListView(posterOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onShareWeChatClick(Bitmap bitmap) {
        if (bitmap == null) return;
        ShareListener shareListener = new ShareListener() {
            @Override
            public void shareSuccess() {
                ToastUtil.showTextViewPrompt("分享成功！");
            }

            @Override
            public void shareFailure(Exception e) {
                ToastUtil.showTextViewPrompt(e.getMessage());
            }

            @Override
            public void shareCancel() {
            }
        };
        ShareUtil.shareImage(mView.getContext(), SharePlatform.WX, bitmap, shareListener);
    }

    @Override
    public void onShareTimelineClick(Bitmap bitmap) {
        if (bitmap == null) return;
        ShareListener shareListener = new ShareListener() {
            @Override
            public void shareSuccess() {
                ToastUtil.showTextViewPrompt("分享成功！");
            }

            @Override
            public void shareFailure(Exception e) {
                ToastUtil.showTextViewPrompt(e.getMessage());
            }

            @Override
            public void shareCancel() {
            }
        };
        ShareUtil.shareImage(mView.getContext(), SharePlatform.WX_TIMELINE, bitmap, shareListener);
    }

    @Override
    public void onSaveSystemClick(Bitmap bitmap) {
        if (bitmap == null) return;
        if (!PermissionsUtils.checkWriteStoragePermission(mView.getContext()))
            return;
        File result = FileUtil.saveShareImageToGallery(mView.getContext(), bitmap);
        ToastUtil.showTextViewPrompt(result != null ? "图片保存成功!请在相册查看" : "图片保存失败");
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
